﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BoletoElectronicoDesktop
{


    public static class LoginDAO
    {

        //devuelve la cantidad de usuarios con el nombre de usuarios especificado
        public static int ExisteUsuario(string usuario)
        {
            SqlConnection connection = new SqlConnection(Funciones.connectionString);
            SqlCommand command = new SqlCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[SELECTASTERISCO].Usuario_Cantidad";
            command.Parameters.AddWithValue("@username", usuario);
            command.CommandTimeout = 10;
            SqlDataReader reader = command.ExecuteReader();
            DataTable dataTable = new DataTable();
            dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataTable.Load(reader);
            connection.Close();
            return Convert.ToInt32(dataTable.Rows[0][0]);
         
        }

        //devuelve el valor del campo habilitado del usuario
        public static int UsuarioHabilitado(string usuario)
        {
            SqlConnection connection = new SqlConnection(Funciones.connectionString);
            SqlCommand command = new SqlCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[SELECTASTERISCO].Usuario_Habilitado";
            command.Parameters.AddWithValue("@username", usuario);
            command.CommandTimeout = 10;
            SqlDataReader reader = command.ExecuteReader();
            DataTable dataTable = new DataTable();
            dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataTable.Load(reader);
            connection.Close();
            return Convert.ToInt32(dataTable.Rows[0][0]);
            
        }

        //devuelve el valor del campo habilitado del usuario
        public static string ObtenerContraseña(string usuario)
        {
            SqlConnection connection = new SqlConnection(Funciones.connectionString);
            SqlCommand command = new SqlCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[SELECTASTERISCO].Usuario_ObtenerContraseña";
            command.Parameters.AddWithValue("@username", usuario);
            command.CommandTimeout = 10;
            SqlDataReader reader = command.ExecuteReader();
            DataTable dataTable = new DataTable();
            dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataTable.Load(reader);
            connection.Close();
            return Convert.ToString(dataTable.Rows[0][0]);



        }
        //actualiza la cantidad de intentos fallidos de login de usuairo
        public static void ActualizarHabilitado(string usuario, string fallidos)
        {
            SqlConnection connection = new SqlConnection(Funciones.connectionString);
            SqlCommand command = new SqlCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[SELECTASTERISCO].Usuario_ActualizarHabilitado";
            command.Parameters.AddWithValue("@username", usuario);
            command.Parameters.AddWithValue("@habilitado", fallidos);
            command.CommandTimeout = 10;
            SqlDataReader reader = command.ExecuteReader();
            connection.Close();
        }

        public static DataTable ObtenerFuncionalidadesUsuario(string username)
        {
            SqlConnection connection = new SqlConnection(Funciones.connectionString);
            SqlCommand command = new SqlCommand();
            connection.Open();
            command.Connection = connection;
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[SELECTASTERISCO].Usuario_Funcionalidades_Cantidad";
            command.Parameters.AddWithValue("@username", username);
            command.CommandTimeout = 10;
            SqlDataReader reader = command.ExecuteReader();
            DataTable dataTable = new DataTable();
            dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataTable.Load(reader);
            connection.Close();
            return dataTable;

        }
    }
}